package com.rongshu.view.discovery.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.UserPostAdapter;
import com.rongshu.view.discovery.presenter.UserPostPresenter;
import com.rongshu.view.penpal.activity.PostDetailsActivity;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/1/16.
 * description：
 */
public class TaPostFragment extends BaseFragment<UserPostPresenter> implements INetView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.empty_hint)
    TextView emptyHint;
    @BindView(R.id.layout_empty)
    RelativeLayout layoutEmpty;
    @BindView(R.id.article_list)
    RecyclerView articleList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.empty_img)
    ImageView emptyImg;
    private int pageIndex = 1;
    private int pageSize = 10;
    private int userId;
    private UserPostAdapter adapter;
    private List<PenpalArticleResponse.ResponseBean> collectedArticles;
    private int clickItemPosition;
    private int INTENT_DETAILS_QUEST = 101;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_user_article;
    }

    @Override
    protected void initViews() {
        Intent intent = getActivity().getIntent();
        if (intent != null) {
            userId = intent.getIntExtra(Constant.USER_ID, 0);
        }

        headMiddle.setText("Ta的帖子");
        emptyHint.setText("Ta还没有帖子哦～");
        emptyImg.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.empty_post));
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        articleList.setLayoutManager(manager);
        collectedArticles = new ArrayList<>();
        adapter = new UserPostAdapter(getActivity(), collectedArticles);
        articleList.setAdapter(adapter);
        adapter.setOnItemClickListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                if (object instanceof Integer) {
                    clickItemPosition = (int) object;
                    Intent intent = new Intent(getActivity(), PostDetailsActivity.class);
                    intent.putExtra(Constant.POST_ID, collectedArticles.get(clickItemPosition).getTopic_id());
                    intent.putExtra("showJoinIcon", true);
                    startActivityForResult(intent, INTENT_DETAILS_QUEST);
                }
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new UserPostPresenter(getActivity()), this);
        refreshView.autoRefresh();
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.resetNoMoreData();
                pageIndex = 1;
                presenter.getPostList(userId, pageIndex, pageSize);
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getPostList(userId, pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.head_left)
    public void onViewClicked() {
        getActivity().finish();
    }


    @Override
    public void success(Object object, int tag) {
        PenpalArticleResponse response = (PenpalArticleResponse) object;
        if (response != null && response.getResponse() != null) {
            List<PenpalArticleResponse.ResponseBean> list = response.getResponse();
            if (pageIndex == 1) {
                collectedArticles.clear();
            }
            if (refreshView.isLoading()) {
                refreshView.finishLoadmore();
            }
            if (refreshView.isRefreshing()) {
                refreshView.finishRefresh();
            }
            collectedArticles.addAll(list);
            adapter.notifyDataSetChanged();
            if (collectedArticles == null || collectedArticles.size() == 0) {
                refreshView.setVisibility(View.GONE);
                layoutEmpty.setVisibility(View.VISIBLE);
            }
            if (list.size() < pageSize) {
                refreshView.setLoadmoreFinished(true);
            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == INTENT_DETAILS_QUEST) {
            int likeCount = data.getIntExtra(Constant.LIKE_COUNT, 0);
            int commentCount = data.getIntExtra(Constant.COMMENT_COUNT, 0);
            collectedArticles.get(clickItemPosition).setLikeCount(likeCount);
            collectedArticles.get(clickItemPosition).setCommentCount(commentCount);
            adapter.notifyItemChanged(clickItemPosition);
        }
    }
}
